package com.itheima.service.db.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.domain.db.AnalysisByDay;
import com.itheima.mapper.AnalysisByDayMapper;
import com.itheima.mapper.LogMapper;
import com.itheima.service.db.AnalysisByDayService;
import com.itheima.util.ComputeUtil;
import com.itheima.vo.AnalysisSummaryVo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.Date;

@Service
public class AnalysisByDayServiceImpl implements AnalysisByDayService {

    @Autowired
    private LogMapper logMapper;

    @Autowired
    private AnalysisByDayMapper analysisByDayMapper;

    @Override
    public void saveAnalysis() {
        // 1.准备基础数据
        // 1-1 今天时间  2021-06-26
        String today = DateUtil.offsetDay(new Date(), 0).toDateStr();
        // 1-2 昨天时间 2021-06-25
        String yesterday = DateUtil.offsetDay(new Date(), -1).toDateStr();

        // 2.查询log数据
        //新注册用户数
        Integer numRegistered = logMapper.findByTimeAndType(today, "0102");
        //活跃用户数
        Integer numActive = logMapper.findByTime(today);
        //登录次数
        Integer numLogin = logMapper.findByTimeAndType(today, "0101");
        //次日留存用户数
        Integer numRetention1d = logMapper.findByTodayAndYesterDay(today, yesterday);

        // 3.判断每日统计分表中是否有此数据
        QueryWrapper<AnalysisByDay> qw = new QueryWrapper<>();
        qw.eq("record_date", today);
        AnalysisByDay analysisByDay = analysisByDayMapper.selectOne(qw);
        if (analysisByDay == null) {// 3-1 没有，新增
            analysisByDay = new AnalysisByDay();
            analysisByDay.setRecordDate(new Date());
            analysisByDay.setNumRegistered(numRegistered);
            analysisByDay.setNumActive(numActive);
            analysisByDay.setNumLogin(numLogin);
            analysisByDay.setNumRetention1d(numRetention1d);
            // 注意一定是insert语句
            analysisByDayMapper.insert(analysisByDay);

        } else {// 3-2 有，更新
            analysisByDay.setNumRegistered(numRegistered);
            analysisByDay.setNumActive(numActive);
            analysisByDay.setNumLogin(numLogin);
            analysisByDay.setNumRetention1d(numRetention1d);
            // 注意一定是update语句
            analysisByDayMapper.updateById(analysisByDay);
        }

    }

    // 统计分析展示
    @Override
    public AnalysisSummaryVo findAnalysisSummaryVo() {
        // 1.准备基础数据
        // 1-1 今天时间  2021-06-26
        String today = DateUtil.offsetDay(new Date(), 0).toDateStr();
        // 1-2 昨天时间 2021-06-25
        String yesterday = DateUtil.offsetDay(new Date(), -1).toDateStr();
        // 1-3 七天前
        String day7 = DateUtil.offsetDay(new Date(), -7).toDateStr();
        // 1-4 30天前
        String day30 = DateUtil.offsetDay(new Date(), -30).toDateStr();

        // 1-5 查询今日统计数据
        AnalysisByDay todayAnalysisByDay = analysisByDayMapper.selectOne(new QueryWrapper<AnalysisByDay>().eq("record_date", today));
        // 1-6 查询昨天统计数据
        AnalysisByDay yesterdayAnalysisByDay = analysisByDayMapper.selectOne(new QueryWrapper<AnalysisByDay>().eq("record_date", yesterday));

        // 2.凑齐9份数据
        // 2-1 累计用户
        Long cumulativeUsers = analysisByDayMapper.findCumulativeUsers();
        // 2-2 过去7天活跃用户
        Long activePassWeek = analysisByDayMapper.findActiveByBetween(day7, today);
        // 2-3 过去30天活跃用户数
        Long activePassMonth = analysisByDayMapper.findActiveByBetween(day30, today);
        // 2-4 今日新增用户数量
        Integer newUsersToday = todayAnalysisByDay.getNumRegistered();
        // 2-5 今日新增用户涨跌率，单位百分数，正数为涨，负数为跌   BigDecimal : 商业数字格式
        BigDecimal newUsersTodayRate = ComputeUtil.computeRate(newUsersToday, yesterdayAnalysisByDay.getNumRegistered());
        // 2-6 今日登录次数
        Integer loginTimesToday = todayAnalysisByDay.getNumLogin();
        // 2-7 今日登录次数涨跌率，单位百分数，正数为涨，负数为跌
        BigDecimal loginTimesTodayRate = ComputeUtil.computeRate(loginTimesToday, yesterdayAnalysisByDay.getNumLogin());
        // 2-8 今日活跃用户数量
        Integer activeUsersToday = todayAnalysisByDay.getNumActive();
        // 2-9 今日活跃用户涨跌率，单位百分数，正数为涨，负数为跌
        BigDecimal activeUsersTodayRate = ComputeUtil.computeRate(activeUsersToday, yesterdayAnalysisByDay.getNumActive());

        // 3.封装vo并返回
        AnalysisSummaryVo analysisSummaryVo = new AnalysisSummaryVo();
        analysisSummaryVo.setCumulativeUsers(cumulativeUsers);
        analysisSummaryVo.setActivePassMonth(activePassMonth);
        analysisSummaryVo.setActivePassWeek(activePassWeek);
        analysisSummaryVo.setNewUsersToday(newUsersToday.longValue());
        analysisSummaryVo.setNewUsersTodayRate(newUsersTodayRate);
        analysisSummaryVo.setLoginTimesToday(loginTimesToday.longValue());
        analysisSummaryVo.setLoginTimesTodayRate(loginTimesTodayRate);
        analysisSummaryVo.setActiveUsersToday(activeUsersToday.longValue());
        analysisSummaryVo.setActiveUsersTodayRate(activeUsersTodayRate);
        return analysisSummaryVo;
    }
}
